package com.xq.shop.controller;

import com.xq.shop.dto.EditGoodsDto;
import com.xq.shop.dto.IntegerDto;
import com.xq.shop.dto.QueryGoodsDto;
import com.xq.shop.service.INetGoodsService;
import com.xq.shop.service.IUserService;
import com.xq.shop.vo.NetGoodsDetailVo;
import com.xq.shop.vo.NetGoodsVo;
import com.xq.shop.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import utils.BasePage;
import utils.BaseResult;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author gaoxiaojin
 * @since 2024-07-24
 */
@RestController
@RequestMapping("/net/goods")
public class NetGoodsController {

    private final INetGoodsService netGoodsService;
    private final IUserService userService;

    @Autowired
    public NetGoodsController(INetGoodsService netGoodsService, IUserService userService) {
        this.netGoodsService = netGoodsService;
        this.userService = userService;
    }

    @RequestMapping("/page")
    public BaseResult<BasePage<NetGoodsVo>> page(@RequestBody QueryGoodsDto dto, HttpServletRequest request) {
        userService.getInfo(request);
        BaseResult<BasePage<NetGoodsVo>> result = new BaseResult<>();
        BasePage<NetGoodsVo> page = netGoodsService.pageQuery(dto);
        result.initializeSuccess(page);
        return result;
    }

    @RequestMapping("/detail")
    public BaseResult<NetGoodsDetailVo> detail(@RequestBody @Validated IntegerDto dto, HttpServletRequest request) {
        userService.getInfo(request);
        BaseResult<NetGoodsDetailVo> result = new BaseResult<>();
        NetGoodsDetailVo vo = netGoodsService.detail(dto);
        result.initializeSuccess(vo);
        return result;
    }

    @RequestMapping("/edit")
    public BaseResult<Void> edit(@RequestBody @Validated EditGoodsDto dto, HttpServletRequest request) {
        UserVo userVo = userService.getInfo(request);
        BaseResult<Void> result = new BaseResult<>();
        netGoodsService.edit(dto, userVo.getId());
        result.initializeSuccess();
        return result;
    }

    @RequestMapping("/remove")
    public BaseResult<Void> remove(@RequestBody @Validated IntegerDto dto, HttpServletRequest request) {
        UserVo userVo = userService.getInfo(request);
        BaseResult<Void> result = new BaseResult<>();
        netGoodsService.removeGoods(dto, userVo.getId());
        result.initializeSuccess();
        return result;
    }

}
